package com.sea.physical.service.impl;

import com.sea.physical.entity.*;
import com.sea.physical.mapper.CreatePlanSupplierFileMapper;
import com.sea.physical.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CreatePlanSupplierFileServiceImpl implements CreatePlanSupplierFileService {

    @Autowired
    private CreatePlanSupplierFileMapper createPlanSupplierFileMapper;
    @Autowired
    private OAuth2ClientService oAuth2ClientService;

    @Override
    public Integer save(CreatePlanSupplierFile createPlanSupplierFile) {
        return createPlanSupplierFileMapper.insert(createPlanSupplierFile);
    }

    @Override
    public List<CreatePlanSupplierFile> findByCreatePlanNo(String createPlanNo) {
        return createPlanSupplierFileMapper.findByCreatePlanNo(createPlanNo);
    }

    @Override
    public Integer deleteByCreatePlanNo(String createPlanNo) throws Exception {

        List<CreatePlanSupplierFile> list = findByCreatePlanNo(createPlanNo);
        String accessToken = oAuth2ClientService.getToken();
        if (null != list && list.size() != 0) {
            for (CreatePlanSupplierFile createPlanSupplierFile : list) {
                String requestId = createPlanSupplierFile.getRequestId();

                if (StringUtils.isNotBlank(accessToken) && StringUtils.isNotBlank(requestId)) {
                    String s = oAuth2ClientService.doForceDrawBack(accessToken, "1100795470840750109", requestId);
                    System.out.println(s);
                    String s1 = oAuth2ClientService.deleteRequest(accessToken, "1100795470840750109", requestId);
                    System.out.println(s1);
                }
            }
        }
        return createPlanSupplierFileMapper.deleteByCreatePlanNo(createPlanNo);
    }

    @Override
    public Integer updateRequestId(String requestId, String createPlanNo,String supplierName) {
        return createPlanSupplierFileMapper.updateRequestId(requestId,createPlanNo,supplierName);
    }
}
